package com.wanghua.spring.listeners;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/*
* 监听对象的创建与销毁。
* HttpSessionListener, ServletContextListener, ServletRequestListener 分别用于监听 session、context、request 的创建与销毁。
* 此 ListenerTest 类需要配置到 web.xml 中。
* */
public class ListenerTest implements HttpSessionListener, ServletContextListener, ServletRequestListener {
    Log log = LogFactory.getLog(getClass());

    // 创建 session 时被调用
    public void sessionCreated(HttpSessionEvent se) {
        HttpSession session = se.getSession();
        log.info("新创建一个session, ID为: " + session.getId());
    }

    // 销毁 session 前被调用
    public void sessionDestroyed(HttpSessionEvent se) {
        HttpSession session = se.getSession();
        log.info("销毁一个session, ID为: " + session.getId());
    }

    // 加载 context 上下文时被调用
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
        log.info("即将启动" + servletContext.getContextPath());
    }

    // 卸载 context 上下文时被调用
    public void contextDestroyed(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
        log.info("即将关闭" + servletContext.getContextPath());
    }

    // 创建 request 时被调用
    public void requestInitialized(ServletRequestEvent sre) {
        HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();

        String uri = request.getRequestURI();
        uri = request.getQueryString() == null ? uri : (uri + "?" + request.getQueryString());

        request.setAttribute("dateCreated", System.currentTimeMillis());

        log.info("IP " + request.getRemoteAddr() + " 请求 " + uri);
    }

    // 销毁 request 时被调用
    public void requestDestroyed(ServletRequestEvent sre) {
        HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();

        long time = System.currentTimeMillis() - (Long) request.getAttribute("dateCreated");

        log.info(request.getRemoteAddr() + "请求处理结束, 用时" + time + "毫秒. ");
    }
}
